//
//  PriceViewController.m
//  SAT_NET
//
//  Created by lanou3g on 16/3/3.
//  Copyright © 2016年 郭丹. All rights reserved.
//

#import "PriceViewController.h"
#import "ProductPriceModel.h"
#import <UIImageView+WebCache.h>

@interface PriceViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, strong)NSMutableDictionary *dicAllData;



@end

@implementation PriceViewController

- (void)loadView {
    self.priceV = [[PriceView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.priceV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [self setDataWithUrl:kNotebookListUrl];
    [self.priceV.leftTableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"left_id"];
    [self.priceV.rightTableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"right_id"];
    
    self.priceV.rightTableV.delegate = self;
    self.priceV.rightTableV.dataSource = self;
    self.priceV.leftTableV.delegate = self;
    self.priceV.leftTableV.dataSource = self;

    

    
    self.leftArray = [NSMutableArray arrayWithObjects:@"最近浏览",@"手机",@"笔记本电脑",@"平板电脑", nil];
    // 首次进入
   
}

- (void)setDataWithUrl:(NSString *)url {
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            self.dicAllData = [NSMutableDictionary dictionary];
            NSDictionary *dicTem = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            for (NSString *key in dicTem.allKeys) {
                NSMutableArray *arrTem = [NSMutableArray array];
                if ([key isEqualToString:@"rank"] == YES) {
                    for (NSDictionary *dic in dicTem[key]) {
                        
                        ProductPriceModel *productPM = [[ProductPriceModel alloc] init];
                        [productPM setValuesForKeysWithDictionary:dic];
                        [arrTem addObject:productPM];
                    }
                    [self.dicAllData setValue:arrTem forKey:@"热门品牌"];
                    
                } else {
                    NSDictionary *dicABC = dicTem[key];
                    for (NSString *keyabc in [dicABC allKeys]) {
                        NSMutableArray *arr = [NSMutableArray array];
                        for (NSDictionary *dicNum in dicABC[keyabc]) {
                            ProductPriceModel *productPM = [[ProductPriceModel alloc] init];
                            [productPM setValuesForKeysWithDictionary:dicNum];
                            [arr addObject:productPM];
                        }
                        [self.dicAllData setValue:arr forKey:keyabc];
                    }
                }
                
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.priceV.rightTableV reloadData];
                NSLog(@"%ld",self.dicAllData.allKeys.count);
            });
        }
    }];
    [task resume];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.priceV.leftTableV == tableView) {
        return self.leftArray.count;
    } else {
        return [self.rightArray[section] count];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView.tag == 101) {
        return 1;
    } else {
        return self.rightArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 102) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"right_id" forIndexPath:indexPath];
        NSArray *array = [self.dicAllData valueForKey:self.dicAllData.allKeys[indexPath.section]];
        ProductPriceModel *productPM = array[indexPath.row];
        [cell.imageView sd_setImageWithURL:productPM.picUrl placeholderImage:[UIImage imageNamed:@"cancel"]];
        cell.textLabel.text = productPM.name;
        
        return cell;
    } else {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"left_id"];
        cell.textLabel.text = self.leftArray[indexPath.row];
        cell.backgroundColor = [UIColor cyanColor];
        return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 101) {
        
        // set array for the right tableView
//        self.rightArray = ;
        [self.priceV.rightTableV reloadData];
    } else {
        // goto the detailView
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
